package com.yt.supplier.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.supplier.models.bo.ProductCategorySaveBO;
import com.yt.supplier.models.dto.ProductCategoryDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import com.yt.supplier.models.service.IProductCategoryService;
import com.yt.supplier.models.bo.ProductCategoryBO;
import com.yt.supplier.models.dto.ProductCategoryDTO;

import java.util.List;

/**
 * 产品分类信息控制器
 *
 * @author lizicong
 * @Date 2025-09-10 10:09:27
 */

@RestController
@RequestMapping("/api/productCategory")
@Slf4j
@Api(value = "ProductCategoryApiController",tags = "产品分类信息Api接口")
public class ProductCategoryApiController {

    @DubboReference
    private IProductCategoryService productCategoryService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ProductCategoryDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        ProductCategoryDTO dto = productCategoryService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "产品分类信息列表",notes = "产品分类信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<ProductCategoryDTO>> queryPageList(ProductCategoryBO bo){
        Wrapper<ProductCategoryDTO> wrapper = new EntityWrapper<>();
                if (bo.getStatus() != null){
                    wrapper.eq("status", bo.getStatus());
                }
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<ProductCategoryDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = productCategoryService.selectPage(page, wrapper);

        return new Result<>(page);
    }
    @ApiOperation(value = "添加产品分类信息",notes = "添加产品分类信息")
    @PostMapping(value = "/add")
    public Result insert(@RequestBody ProductCategorySaveBO saveBO) {
        System.out.println("saveBO: " + saveBO);
        boolean flag = productCategoryService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }
    @ApiOperation(value = "修改产品分类信息",notes = "修改产品分类信息")
    @PostMapping("/update")
    public Result update(@RequestBody ProductCategorySaveBO saveBO){
        ProductCategoryDTO dto = new ProductCategoryDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = productCategoryService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }
    @ApiOperation(value = "删除产品分类信息",notes = "删除产品分类信息")
    @ApiImplicitParam(name = "id",value = "产品分类信息id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = productCategoryService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }
    @ApiOperation(value = "产品分类信息列表",notes = "产品分类信息列表")
    @PostMapping("/productCategorylist")
    public Result<List<ProductCategoryDTO>> supplierlist(){

        List<ProductCategoryDTO> productCategoryDTOList = productCategoryService.selectList(null);
        if (productCategoryDTOList!= null){
            System.out.println(" ProductCategoryDTOList "+productCategoryDTOList);
            return new Result<>(productCategoryDTOList);
        } else {
            return new Result<>(CodeEnum.FAIL_100005);
        }
    }
    @ApiOperation(value = "搜索产品分类信息", notes = "根据关键词搜索产品分类名称")
    @ApiImplicitParam(name = "keyword", value = "搜索关键词（产品分类名称）",
            required = true, paramType = "query", dataType = "String")
    @PostMapping("/searchproductCategory") // 路径无参数，固定为 /searchsupplier
    public Result<List<ProductCategoryDTO>> searchproductCategory(@RequestParam("keyword") String keyword) { // 接收查询参数
        System.out.println("keyword: " + keyword);
        // 注意：这里需要手动拼接 % 实现模糊查询（之前的问题）
        List<ProductCategoryDTO> productCategoryDTOlist = productCategoryService.SearchproductCategorylist(keyword);
        if (productCategoryDTOlist == null) {
            return new Result<>(CodeEnum.FAIL_100005);
        }
        System.out.println("productCategoryDTOlist: " + productCategoryDTOlist);
        return new Result<>(productCategoryDTOlist);
    }
}
